package com.solocredit.finance.ui.main.fm

import android.view.LayoutInflater
import android.view.View
import com.solocredit.finance.R
import com.solocredit.finance.base.utils.TitleBarUtil
import com.solocredit.finance.databinding.FmMullaUserBinding
import com.solocredit.finance.http.HttpHelper
import com.solocredit.finance.mvp.ProFm
import com.solocredit.finance.ui.MullaPaPoyAct
import com.solocredit.finance.ui.user.act.MullaUserCheckInAct
import com.solocredit.finance.ui.user.act.MullaUserCouponAct
import com.solocredit.finance.ui.user.act.MullaUserFeedbackAct
import com.solocredit.finance.ui.user.act.MullaUserLuckyDrawAct
import com.solocredit.finance.ui.user.act.MullaUserMessageAct
import com.solocredit.finance.ui.user.act.MullaUserPointsAct
import com.solocredit.finance.ui.user.act.MullaUserProfileAct
import com.solocredit.finance.ui.user.act.MullaUserSettingAct
import com.solocredit.finance.ui.user.act.MullaUserVipAct
import com.solocredit.finance.utils.event.MullaEventData
import com.solocredit.finance.utils.event.MullaEventType
import com.solocredit.finance.utils.helper.MullaCustomConfigsHelper
import com.solocredit.finance.utils.helper.MullaCustomDataHelper

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/7/26 16:57
 * @Annotation：个人中心
 */
class MullaUserFm: ProFm() {
    private var _binding: FmMullaUserBinding? = null
    private val binding get() = _binding ?: throw IllegalStateException(
        "Cannot access binding because it is null. Either the view hasn't been created yet, or it has already been destroyed."
    )
    override fun getMullaRootView(inflater: LayoutInflater): View {
        _binding = FmMullaUserBinding.inflate(inflater)
        return binding.root
    }

    override fun initMullaView() {
        _binding?.contentLayout?.setPadding(0, TitleBarUtil.getStatusBarHeight(), 0, 0)

        //用户使用条款
        val termsUseUrl = MullaCustomConfigsHelper.getMullaTermsUseLink()
        if (termsUseUrl.isEmpty()){
            _binding?.termsUseLayout?.visibility = View.INVISIBLE
        }
    }

    override fun initMullaListener() {
        //客服
        _binding?.imgKefu?.setOnClickListener {
            showMullaKefuDialog("User Center Page")
            HttpHelper.addMullaEventAction(MullaEventType.CLICK.MULLA_CLICK_CUSTOMER,"User Center Page")
        }

        //消息
        _binding?.imgMessage?.setOnClickListener {
            onIntent(MullaUserMessageAct::class.java)
        }

        //个人简介
        _binding?.imgAvatar?.setOnClickListener {
            onIntent(MullaUserProfileAct::class.java)
        }

        //优惠券列表
        _binding?.couponLayout?.setOnClickListener {
            onIntent(MullaUserCouponAct::class.java)
        }

        //积分明细
        _binding?.pointsLayout?.setOnClickListener {
            onIntent(MullaUserPointsAct::class.java)
        }

        //check-in
        _binding?.checkInLayout?.setOnClickListener {
            onIntent(MullaUserCheckInAct::class.java)
        }
        //抽奖
        _binding?.luckydrawLayout?.setOnClickListener {
            onIntent(MullaUserLuckyDrawAct::class.java)
        }
        //VIP等级中心
        _binding?.vipLayout?.setOnClickListener {
            onIntent(MullaUserVipAct::class.java)
        }
        //联系我们
        _binding?.contactUsLayout?.setOnClickListener {
            _binding?.imgKefu?.callOnClick()
        }
        //意见反馈
        _binding?.adviceLayout?.setOnClickListener {
            MullaUserFeedbackAct.start(requireContext(),"")
        }
        //设置
        _binding?.settingLayout?.setOnClickListener {
            onIntent(MullaUserSettingAct::class.java)
        }
        //用户使用协议
        _binding?.termsUseLayout?.setOnClickListener {
            val showTitle = getString(R.string.terms_of_use)
            val linkUrl = MullaCustomConfigsHelper.getMullaTermsUseLink()
            MullaPaPoyAct.start(requireContext(),showTitle, linkUrl)
        }
        //隐私政策
        _binding?.privacyPolicyLayout?.setOnClickListener {
            val showTitle = getString(R.string.privacy_policy_title)
//            val linkUrl = "https://www.vistafinance.net/pri.html"
            val linkUrl = MullaCustomConfigsHelper.getMullsPrivacyPolicyLink()
            MullaPaPoyAct.start(requireContext(),showTitle, linkUrl)
        }
    }

    override fun initMullaData() {
        _binding?.textPoints?.text = getMullaUserPoints().toString()
        val vipGrade = getMullaUserVip()
        _binding?.textVipGrade?.text = String.format(getString(R.string.vip_grade),vipGrade.toString())
        when(vipGrade){
            1 -> {
                _binding?.imgVip?.setImageResource(R.mipmap.mulla_user_vip1_icon)
            }
            2 -> {
                _binding?.imgVip?.setImageResource(R.mipmap.mulla_user_vip2_icon)
            }
            else ->{
                _binding?.imgVip?.setImageResource(R.mipmap.mulla_user_vip3_icon)
            }
        }

        //设置用户昵称和头像
        showMullaUserInfo()

        //是否有新版本
        val isUpdate = MullaCustomConfigsHelper.isFlexiMolaUpdate()
        if (isUpdate){
            _binding?.viewUserNewVersion?.visibility = View.VISIBLE
        } else {
            _binding?.viewUserNewVersion?.visibility = View.GONE
        }

        //牌照信息
        val licenseInfo = MullaCustomConfigsHelper.getMullaLicenseInfo()
        if (licenseInfo == null){
            _binding?.licenseLayout?.removeAllViews()
            _binding?.licenseLayout?.visibility = View.GONE
        } else {
            _binding?.licenseLayout?.removeAllViews()
            _binding?.licenseLayout?.addView(licenseView())
            _binding?.licenseLayout?.visibility = View.VISIBLE
        }
        HttpHelper.addMullaEventAction(MullaEventType.PAGE.MULLA_PAGE_USER_CENTER_START)
    }

    /**
     * 设置用户头像和昵称
     */
    private fun showMullaUserInfo(){
        _binding?.imgAvatar?.setImageResource(MullaCustomDataHelper.getMullaCustomUserAvatar())
        _binding?.textUserName?.text = MullaCustomDataHelper.getMullaCustomUserName()
    }

    override fun onMullaEvent(event: MullaEventData) {
        when(event.eventType){
            MullaEventData.TYPE.MULLA_POINTS_REFRESHED -> { //刷新积分完成
                _binding?.textPoints?.text = getMullaUserPoints().toString()
            }
            MullaEventData.TYPE.MULLA_COUPON_REFRESHED -> { //刷新优惠券完成
                _binding?.textCouponCount?.text = getMullaUserCouponCount().toString()
            }
            MullaEventData.TYPE.MULLA_CUSTOM_DATA_REFRESHED -> {//自定义数据刷新完成
                //设置用户昵称和头像
                showMullaUserInfo()
            }
            else -> {

            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        HttpHelper.addMullaEventAction(MullaEventType.PAGE.MULLA_PAGE_USER_CENTER_END)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}